package com.zys.el.time.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zys.el.time.common.exception.sql.SqlException;
import com.zys.el.time.entity.Channel;
import com.zys.el.time.entity.ChannelSubscribe;
import com.zys.el.time.entity.User;
import com.zys.el.time.fegin.UserClient;
import com.zys.el.time.mapper.ChannelMapper;
import com.zys.el.time.service.ChannelService;
import com.zys.el.time.service.ChannelSubscribeService;
import com.zys.el.time.vojo.ChannelVo;
import com.zys.el.time.vojo.UserVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service
public class ChannelServiceImpl extends ServiceImpl<ChannelMapper, Channel>
        implements ChannelService {
    @Resource
    private UserClient userClient;
    @Resource
    private ChannelSubscribeService channelSubscribeService;

    @Override
    public List<ChannelVo> getByUserId(Long userId) {
        List<ChannelVo> voList = this.getBaseMapper().getByUserId(userId).stream().map(c -> {
            ChannelVo channelVo = new ChannelVo();
            BeanUtils.copyProperties(c, channelVo);
            Long count = channelSubscribeService.query().eq("channel_id", c.getId()).count();
            channelVo.setMember(Math.toIntExact(count));
            return channelVo;
        }).collect(Collectors.toList());
        return voList;
    }

    @Override
    public ChannelVo getChannelInfoById(Long id, Long userId) {
        Channel channel = this.getById(id);
        boolean exists = channelSubscribeService.query().eq("user_id", userId).eq("channel_id", id).exists();
        ChannelVo channelVo = new ChannelVo();
        BeanUtils.copyProperties(channel, channelVo);

        Long count = channelSubscribeService.query().eq("channel_id", id).count();
        channelVo.setMember(Math.toIntExact(count));

        channelVo.setIsJoin(exists);
        // 设置话题主用户信息
        Map<Long, User> userMap = userClient.getUserListByBatchId(Collections.singletonList(channel.getOwnerId()));
        channelVo.setOwner(new UserVo(userMap.get(channel.getOwnerId())));

        return channelVo;
    }

    @Override
    @Transactional
    public void saveAndSubscribe(Channel channel) {
        channel.setMember(1);
        boolean b1 = this.save(channel);
        if (!b1) {
            throw new SqlException("添加频道异常");
        }

        // 将频道主订阅本频道
        boolean b2 = channelSubscribeService.save(new ChannelSubscribe(channel.getOwnerId(), channel.getId()));
        if (!b2) {
            throw new SqlException("订阅频道异常");
        }

    }

    @Override
    @Transactional
    public void removeByIdWithSubscribe(Long channelId) {
        boolean b1 = this.removeById(channelId);
        if (!b1) {
            throw new SqlException("删除频道异常");
        }

        // 取消订阅
        QueryWrapper<ChannelSubscribe> qw = new QueryWrapper<>();
        qw.lambda().eq(ChannelSubscribe::getChannelId, channelId);
        boolean b2 = channelSubscribeService.remove(qw);
        if (!b2) {
            throw new SqlException("取消订阅异常");
        }
    }

}
